@tool
extends  EditorProperty

const TAG_ROW_COMP = preload("res://addons/gameplay-ability-system/Scripts/Inspector/components/tag_comp/TagRowComp.tscn")

var tag_row_comp:Node
var tag_list_name:String

func _init(type_name:String) -> void:
	tag_list_name = type_name


func _ready() -> void:
	tag_row_comp = TAG_ROW_COMP.instantiate()
	tag_row_comp.tags = match_ability_tags().duplicate()
	tag_row_comp.add_tag.connect(_on_add_tag)
	tag_row_comp.remove_tag.connect(_on_remove_tag)
	add_child(tag_row_comp)


func _on_add_tag(new_tag):
	var new_tags = match_ability_tags().duplicate() as Array
	new_tags.append(new_tag)
	match_ability_tags(new_tags)
	tag_row_comp.draw_tags(new_tags)

func _on_remove_tag(tag_to_remove:String):
	var new_tags = match_ability_tags().duplicate() as Array
	new_tags.erase(tag_to_remove)
	match_ability_tags(new_tags)
	tag_row_comp.draw_tags(new_tags)


# 根据被初始化参数赋值的tag_list_name获取对应的 ability 的 tags,
# 若调用次方法时携带参数，则会先将对应 tags 赋值为参数的值再返回
func match_ability_tags(new_tag_list:Array[String] = []) -> Array:
	var ability = get_edited_object() as Ability
	match tag_list_name:
		"tags_learn_required":
			if new_tag_list.size() > 0:
				ability.tags_learn_required = new_tag_list
			return ability.tags_learn_required
		"tags_activation":
			if new_tag_list.size() > 0:
				ability.tags_activation = new_tag_list
			return ability.tags_activation
		_:
			printerr("不存在这种tag列表，已返回空数组（检查初始化参数是否在 ability 有对应tag列表）")
			return []
